package com.bytecodezz.app

import android.content.Intent
import android.graphics.Bitmap
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Environment
import android.view.View
import android.widget.ImageView
import android.widget.SeekBar
import android.widget.TextView
import androidx.core.net.toFile
import com.bytecodezz.app.photoshop.loader.core.PhotoshopLoader
import com.bytecodezz.app.photoshop.loader.listener.BitmapLoaderListener
import com.bytecodezz.photoshop.compressor.CompressorTools
import com.bytecodezz.photoshop.compressor.OnCompressorListener
import java.io.File
import java.lang.Exception
import kotlin.math.log

class PhotoCompressorActivity : AppCompatActivity() {
    var raw_imv: ImageView? = null
    var compressor_imv: ImageView? = null
    var quality_seek_bar: SeekBar? = null
    var quality_tv: TextView? = null
    var raw_kb_tv: TextView? = null
    var kb_tv: TextView? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_photo_compressor)
        raw_imv = findViewById(R.id.raw_imv)
        compressor_imv = findViewById(R.id.compressor_imv)
        quality_seek_bar = findViewById(R.id.quality_seek_bar)
        quality_tv = findViewById(R.id.quality_tv)
        raw_kb_tv = findViewById(R.id.raw_kb_tv)
        kb_tv = findViewById(R.id.kb_tv)

        quality_seek_bar!!.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                quality_tv!!.text = "$progress"
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
            }

        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == 111 && resultCode == RESULT_OK) {
            val selectedFile = data?.data //The uri with the location of the file
            val filepath: String =
                Environment.getExternalStorageDirectory().absolutePath + File.separator.toString() + "###" + quality_seek_bar!!.progress + "###" + selectedFile!!.path!!.substring(
                    selectedFile!!.path!!.length - 8
                ) + ".jpg"
            PhotoshopLoader.with(this@PhotoCompressorActivity)
                .load(selectedFile!!)
                .asBitmap()
                .addListener(object : BitmapLoaderListener {
                    override fun onLoadFailed(e: Exception?, model: Any?): Boolean {
                        return false
                    }

                    override fun onResourceReady(model: Any?, resource: Bitmap?): Boolean {
                        raw_imv!!.setImageBitmap(resource)
                        raw_kb_tv!!.text = "${resource!!.byteCount / 1024 / 1024}"

                        CompressorTools().compressBitmap(
                            resource!!,
                            quality_seek_bar!!.progress,
                            filepath,
                            false,
                            object : OnCompressorListener {
                                override fun onCompressorResult(
                                    rawBitmap: Bitmap?,
                                    outputPath: String?
                                ) {
                                    PhotoshopLoader.with(this@PhotoCompressorActivity)
                                        .load(outputPath!!)
                                        .into(compressor_imv!!)
                                    var file: File = File(outputPath!!)
                                    if (file.exists() && file.isFile()) {
                                        kb_tv!!.text = "${file.length()}"
                                    }
                                }

                            })

                        return false
                    }

                }).into()
        }
    }

    fun selectPhoto(view: View) {
        //打开相册选择图片
        val intent = Intent()
            .setType("*/*")
            .setAction(Intent.ACTION_GET_CONTENT)

        startActivityForResult(Intent.createChooser(intent, "Select a file"), 111)
    }
}